package models

import (
	"gopkg.in/mgo.v2/bson"
	"kukukaki/db"
	"fmt"
	 "crypto/md5"
    "encoding/hex"
)


type IngredientInfo struct {
	ID        string `json:"id"  bson:"_id"`
	Name      string `json:"name"  bson:"name"`
	NameCN      string `json:"namecn"  bson:"namecn"`
	INCI      string `json:"inci"  bson:"inci"`
	Alias     string `json:"alias"  bson:"alias"`
	CAS    	  string `json:"cas"  bson:"cas"`
	Use       string `json:"use"  bson:"use"`
	EWG       string `json:"ewg"  bson:"ewg"`
	Caution   string `json:"caution"  bson:"caution"`  //警告
	Overview  string `json:"overview"  bson:"overview"`
	RegTime  string `json:"regTime"  bson:"regTime"`
	NameJP    string `json:"namejp"  bson:"namejp"`
	
}


type II struct {
	Code        string `json:"id"  bson:"code"`
	CN      string `json:"name"  bson:"cn"`
	EN      string `json:"inci"  bson:"en"`
	Status    string `json:"status"  bson:"status"`
}

var IiData []II

var IiData2 []IngredientInfo

//生成32位md5字串
func Md5(s string) string {
    h := md5.New()
    h.Write([]byte(s))
    return hex.EncodeToString(h.Sum(nil))
}

func GetIiData(s string)(count int){
	 con := db.GetDataBase().C("IngredientInfo")
	 con.Find(bson.M{"status": s}).Sort("code").All(&IiData)
	 return len(IiData)
}

func GetIiData2()(count int){
	 con := db.GetDataBase().C("Ingredient")
	 con.Find(bson.M{}).Sort("name").All(&IiData2)
	 return len(IiData)
}

func AddNameCn(id *string,cn *string) (r int ,err error) {
    con := db.GetDataBase().C("Ingredient")
 
     fmt.Println("-_id-:",*id)
     err = con.Update(bson.D{{"_id", *id}}, bson.M{"$set": bson.M{"namecn": *cn}})
    //fmt.Println("-UpsertId-",changeInfo)
    if err  != nil{
    	return 0,err
    }

    return 1,err
}

func UpsertIdII(i *IngredientInfo) (r int ,err error) {
    con := db.GetDataBase().C("Ingredient")
    i.ID = Md5(i.Name)
     fmt.Println("-_id-",i.ID)
    changeInfo, err := con.UpsertId(i.ID, bson.M{"$set":i})
    fmt.Println("-UpsertId-",changeInfo)
    if changeInfo  == nil{
    	return 0,err
    }

    return 1,err
}
func FindIIForName(names *[]string,lang *string) (data []IngredientInfo ,err error) {
	var list  []IngredientInfo
	con := db.GetDataBase().C("Ingredient")
	//k := len(*names)
	
	//ret := make([]interface{}, k)
	
	//ddd :=*names
	//多条件拼接查询 不区分大小写
	field := "namecn"
	
	if *lang == "en"{
		field = "inci"
	}else if *lang == "jp"{
		field = "namejp"
	}
	
	//for index := 0; index < k; index++ {
	//	ret[index]= bson.M{field: bson.M{"$regex":"^"+ddd[index]+"$","$options":"i"}}
	//}

	//con.Find(bson.M{"$or": ret}).All(&list)
	con.Find(bson.M{field: bson.M{"$in": names}}).All(&list)
    return list,err
}

func FindIIAllName() (names []IngredientInfo ,err error) {
	var list  []IngredientInfo
	con := db.GetDataBase().C("Ingredient")
	con.Find(bson.M{}).Select(bson.M{"namecn":1,"_id":0}).All(&list)
    return list,err
}

func FindIISourceAllName() (names []II ,err error) {
	var list  []II
	con := db.GetDataBase().C("IngredientInfo")
	con.Find(bson.M{}).Select(bson.M{"cn":1,"_id":0}).All(&list)
    return list,err
}

func FindIISourceAllNameEn() (names []II ,err error) {
	var list  []II
	con := db.GetDataBase().C("IngredientInfo")
	con.Find(bson.M{}).Select(bson.M{"en":1,"_id":0}).All(&list)
    return list,err
}



func FindIIAllNameEn() (names []IngredientInfo ,err error) {
	var list  []IngredientInfo
	con := db.GetDataBase().C("Ingredient")
	con.Find(bson.M{}).Select(bson.M{"inci":1,"_id":0}).All(&list)
    return list,err
}


func FindIIAllNameJp() (names []IngredientInfo ,err error) {
	var list  []IngredientInfo
	con := db.GetDataBase().C("Ingredient")
	con.Find(bson.M{}).Select(bson.M{"namejp":1,"_id":0}).All(&list)
    return list,err
}
func FindLikeII(name string) (data []IngredientInfo ,err error) {
	var list  []IngredientInfo
	con := db.GetDataBase().C("Ingredient")
	if len(name) >0 {
		con.Find(bson.M{"name": bson.M{"$regex": name, "$options": "$i"}}).All(&list)
	}else{
		con.Find(nil).All(&list)
	}
	
    return list,err
}